<?php

namespace Tool;

use Think\Controller;
/**
 * 二维码生成
 */
class Qrcode{
    private $model;
    public function __construct(){  
        $this->model=M('equipment');
    }
    public function index($data) {
        $data1 = json_encode($data);
        $data1 = base64_encode($data1);
        $this->model->where($data)->save(array('cnf'=>$data1));
        //Vendor('phpqrcode.qrlib');
        require __DIR__.'/phpqrcode/qrlib.php';
        // 纠错级别：L、M、Q、H
        $level = 'L';
        // 点的大小：1到10,用于手机端4就可以了
        $size = 4;
        $path = dirname(dirname(dirname(__FILE__))).'/';
        $fileName = 'Data/Qrcode/'.time().rand(1000000000,9999999999).'.png';
        //echo $fileName;
        \QRcode::png($data1,$path.$fileName, $level, $size);
        return $fileName;
    }
    /**
     * 生成二维码
     */
    public function qrcode($id) {
        $map['equ_id'] = $id;
        $data = $this->model->field('equ_id,userAccount')->where($map)->find();
        if(empty($data)){
            $this->error('抱歉，没有该设备，操作不当！');
        }
        $url['qrcode'] = $this->index($data);
        if(empty($url)){
            $this->error('抱歉，未能生成二维码！');
        }
        $result = $this->model->where($map)->save($url);
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }
    }
    /**
     * 解码（二维码)
     */
    public static function deqrcode($data){
        $data = base64_decode($data);
        return json_decode($data);
    }
}
